Skip to content

PostgreSQL: Add support for DO statement - #2618

Open
devtechedge wants to merge 3 commits into
apache:mainfrom
devtechedge:postgres-do-statement
Open

devtechedge wants to merge 3 commits into
apache:mainfrom
devtechedge:postgres-do-statement

Conversation

@devtechedge

@devtechedge devtechedge commented Oct 7, 2026 •

Copy link
Copy Markdown

DO blocks like DO $$BEGIN RAISE NOTICE 'hi'; END$$ currently fail with "Expected: an SQL statement, found: DO".

This adds Statement::Do { language, code } for PostgreSQL's DO statement, gated behind a new Dialect::supports_do_statement() that is enabled for PostgreSQL.

The code block must be a string literal (single-quoted, dollar-quoted, or E''), and LANGUAGE lang is accepted either before or after it, as the PostgreSQL grammar allows.

Display normalizes to the documented DO [ LANGUAGE lang ] code order, and a repeated LANGUAGE is rejected.

Tests in tests/sqlparser_common.rs cover both orders, the round trip, the error cases, and dialects without support.

The implementation and tests were drafted with an AI coding assistant (Grok 4.7 effort: xHigh), and I reviewed the diff, ran the test suite and the fuzzer, and stand by the change.

Fixes #2609

@LucaCappelletti94

Copy link
Copy Markdown
Contributor

Please add examples of this syntax to the fuzzer corpus

@LucaCappelletti94 LucaCappelletti94 added enhancement New feature or request waiting on reviewer PR needs to be reviewed waiting on contributor The review needs further refinements by its author and removed waiting on reviewer PR needs to be reviewed labels Oct 12, 2026
@codecov-commenter

codecov-commenter commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.33333% with 5 lines in your changes missing coverage. Please review.
✅ Project coverage is 81.14%. Comparing base (14cbf75) to head (0198d07).

Files with missing lines Patch % Lines
src/ast/mod.rs 66.66% 0 Missing and 2 partials ⚠️
src/parser/mod.rs 88.23% 0 Missing and 2 partials ⚠️
src/ast/spans.rs 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2618   +/-   ##
=======================================
  Coverage   81.14%   81.14%           
=======================================
  Files          42       42           
  Lines       33736    33766   +30     
  Branches    33736    33766   +30     
=======================================
+ Hits        27376    27401   +25     
- Misses       2797     2798    +1     
- Partials     3563     3567    +4     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@devtechedge
devtechedge force-pushed the postgres-do-statement branch from bad1741 to 0198d07 Compare October 12, 2026 16:52
@devtechedge

Copy link
Copy Markdown
Author

@LucaCappelletti94 Good call, the fuzzer should know about this syntax too.

I added five seeds under fuzz/fuzz_seeds/: postgres_do.sql, postgres_do_language_before.sql, postgres_do_language_after.sql, postgres_do_tagged_dollar_quote.sql and postgres_do_single_quoted.sql.

Together they cover a plain $$ body, LANGUAGE plpgsql on either side of the body, a tagged dollar quote with a nested $$ inside, and a single-quoted body.

fmt, clippy and the full test suite pass with them added.

Happy to add or reshape any of them if you had other cases in mind 🙂

Comment thread src/ast/mod.rs Outdated
Comment on lines +4937 to +4942
Do {
/// Optional procedural language name.
language: Option<Ident>,
/// The anonymous code block, a string literal.
code: ValueWithSpan,
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor this to a named struct, as every new statement variant does. I will do another review round after this refactoring as I expect most of the code will change.

Suggested change
Do {
/// Optional procedural language name.
language: Option<Ident>,
/// The anonymous code block, a string literal.
code: ValueWithSpan,
},
Do(DoStatement),

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @LucaCappelletti94!

I refactored it into a named DoStatement struct (Statement::Do(DoStatement)), with its own Display and From<DoStatement> for Statement, following the pattern of the other statement structs.

I also gave it a Spanned impl covering the language and code block, with a small span test.

Parsing behavior and existing tests are unchanged, and fmt, clippy and the full test suite pass.

Ready for another look whenever you have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request PostgreSQL waiting on contributor The review needs further refinements by its author

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PostgreSQL DO statement

3 participants